<template>
    <el-card style="max-width: 480px;margin:auto auto">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
            <el-form-item label="用户名" prop="userName">
                <el-input v-model="ruleForm.userName" />
            </el-form-item>

            <el-form-item label="密码" prop="userPass">
                <el-input v-model="ruleForm.userPass" type="password" placeholder="请输入密码" show-password />
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    登录
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import router from '@/router'
import { lo } from 'element-plus/es/locales.mjs'

interface RuleForm {
    userName: string
    userPass: string
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    userName: '',
    userPass: ''
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
    userName: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
    ],
    userPass: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')

            axios.post("http://localhost:5002/api/User/UserLogin", ruleForm).then(res => {
                if (res.data.code == 200) {
                    ElMessage.success("登录成功")
                    router.push("/GoodsView")
                    console.log(res.data.data)
                    localStorage.setItem("userName", res.data.data.userName);
                    localStorage.setItem("userReal", res.data.data.userReal)
                    localStorage.setItem("token", res.data.data.token)
                }
                else {
                    ElMessage.error(res.data.meg)
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))
</script>